package com.wswy.wzcx.utils

import io.reactivex.*
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers
import org.reactivestreams.Publisher

object RxUtils {

    inline fun <reified T> io2main():SchedulerTransformer<T,T> = object : SchedulerTransformer<T,T>{
        override fun apply(upstream: Maybe<T>): MaybeSource<T> = upstream.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())

        override fun apply(upstream: Flowable<T>): Publisher<T> = upstream.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())

        override fun apply(upstream: Observable<T>): ObservableSource<T> = upstream.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())

        override fun apply(upstream: Single<T>): SingleSource<T> = upstream.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
    }


    inline fun <reified T> switch2io():SchedulerTransformer<T,T> = object : SchedulerTransformer<T,T>{
        override fun apply(upstream: Maybe<T>): MaybeSource<T> = upstream.subscribeOn(Schedulers.io()).observeOn(Schedulers.io())

        override fun apply(upstream: Flowable<T>): Publisher<T> = upstream.subscribeOn(Schedulers.io()).observeOn(Schedulers.io())

        override fun apply(upstream: Observable<T>): ObservableSource<T> = upstream.subscribeOn(Schedulers.io()).observeOn(Schedulers.io())

        override fun apply(upstream: Single<T>): SingleSource<T> = upstream.subscribeOn(Schedulers.io()).observeOn(Schedulers.io())
    }

    interface SchedulerTransformer<Upstream, Downstream> : FlowableTransformer<Upstream, Downstream>,
            ObservableTransformer<Upstream, Downstream>,
            SingleTransformer<Upstream, Downstream>,
            MaybeTransformer<Upstream, Downstream>




}